<?php


class AddressController extends CommonController
{
    public function defaultsAction()
    {
        json([
            'code' => 1,
            'msg' => '默认收货地址',
            'data' => memberaddressModel::getUserDefaultAddress()
        ], 'json', '', false);
    }

    public function indexAction()
    {
        json(['code' => 1, 'msg' => '收货地址列表', 'data' => memberaddressModel::getUserAddress()]);
    }

    public function areaAction()
    {
        $area = remember('area', 0, function () {
            $data = [
                'provinceData' => [],
                'cityData' => [],
                'areaData' => [],
            ];
            $all_list = areaModel::whereIn('level', [1, 2])
                ->orderBy('id')
                ->selectRaw('id as value, name as label, pid, level')
                ->get();
            foreach ($all_list as $k => $item) {
                if ($item->level == 1 && $item->id <> 820105) {
                    $data['provinceData'][] = $item->toArray();
                }
            }
            foreach ($data['provinceData'] as $pk => $provinceItem) {
                foreach ($all_list as $k => $item) {
                    if ($item->level == 2 && $item->pid == $provinceItem['value']) {
                        $data['cityData'][$pk][] = $item->toArray();
                        unset($all_list[$k]);
                    }
                }
            }
            unset($all_list);
            $mark = 0;
            while ($mark !== false) {
                $mark_start = $mark;
                $all_list = areaModel::where('level', 3)
                    ->orderBy('id')
                    ->selectRaw('id as value, name as label, pid, level')
                    ->offset($mark)
                    ->limit(2000)
                    ->get();
                foreach ($data['provinceData'] as $pk => $provinceItem) {
                    foreach ($data['cityData'][$pk] as $ck => $cityItem) {
                        foreach ($all_list as $k => $item) {
                            if ($item->level == 3 && $item->pid == $cityItem['value']) {
                                $data['areaData'][$pk][$ck][] = $item->toArray();
                                unset($all_list[$k]);
                                $mark++;
                            }
                        }
                    }
                }
                unset($all_list);
                if ($mark == $mark_start) {
                    $mark = false;
                }
            }
            return $data;
        });

        json(['code' => 1, 'msg' => '省市区', 'data' => $area]);
    }

    public function editAction()
    {
        $params = $this->getPost();
        try {
            Validate::check($params, [
                'consignee' => ['role'=>'required', 'msg'=>'收货人'],
                'phone' => ['role'=>'required', 'msg'=>'手机号'],
                'address' => ['role'=>'required', 'msg'=>'详细地址'],
            ]);
            if($params['country']==1){
                Validate::check($params, [
                    'area_text' => ['role'=>'required', 'msg'=>'省市区'],
                ]);
            }
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }

        json(['code' => 1, 'msg' => '编辑地址', 'data' => memberaddressModel::edit($params)]);
    }

    public function infoAction()
    {
        $id = $this->get('id');
        json(['code' => 1, 'msg' => '收货地址信息', 'data' => memberaddressModel::info($id)]);
    }

    public function delAction()
    {
        $id = $this->get('id');
        json(['code' => 1, 'msg' => '删除收货地址', 'data' => memberaddressModel::del($id)]);
    }
}
